<?php
/**
 * @package     Joomla.Plugin
 * @subpackage  System.facebookmeta
 *
 * @copyright   Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

class PlgSystemFacebookmeta extends JPlugin
{


	function onBeforeCompileHead()
	{

		$app = JFactory::getApplication();
		if ($app->isAdmin()) return true;


		$router = JSite::getRouter();
		$query = $router->parse(JURI::getInstance());


		$option = $query['option'];
		$view = $query['view'];
		$layout = $query['layout'];
		$Itemid = $query['Itemid'];
		$catid = $query['catid'];
		$id = $query['id'];

		if($option != 'com_content')
			$view = 'default';

		$fnc = 'setMeta'.ucfirst($view);
		if($view == 'article' || $view == 'category' || $view == 'default'){
			$this->$fnc($option,$view,$layout,$catid,$id,$Itemid,$this->params);
		}

		return true;
		//exit();
	}

	function setMetaDefault($option,$view,$layout,$catid,$id,$Itemid,$params){

		$app = JFactory::getApplication();
		$db = JFactory::getDbo();
		$config = JFactory::getConfig();
		$doc = JFactory::getDocument();

		$doc->setMetaData( 'og:title', $params->get('metatitle'));
		$doc->setMetaData( 'og:site_name', $config->sitename );
		$doc->setMetaData( 'og:url', JUri::getInstance()->toString());
		$doc->setMetaData( 'og:type', 'article');
		$doc->setMetaData( 'og:locale', 'vi_VN');
		$doc->setMetaData( 'og:description', $config->MetaDesc);
		$doc->setMetaData( 'og:image', JUri::base().$params->get('metaimage'));
		$doc->setMetaData( 'og:image:secure_url', JUri::base().$params->get('metaimage'));

		return true;
	}

	function setMetaArticle($option,$view,$layout,$catid,$id,$Itemid,$params){
		$app = JFactory::getApplication();
		$db = JFactory::getDbo();
		$config = JFactory::getConfig();

		$query = 'SELECT * FROM #__content WHERE id = '.$id;
		$contentobj = $db->setQuery($query)->loadObject();

		$doc = JFactory::getDocument();
		$doc->setMetaData( 'og:title', $contentobj->title );
		$doc->setMetaData( 'og:site_name', $config->sitename );
		$doc->setMetaData( 'og:url', JURI::current());
		$doc->setMetaData( 'og:type', 'article');
		$doc->setMetaData( 'og:locale', 'vi_VN');


		$query = 'SELECT * FROM #__categories WHERE id = ' . intval($catid);
		$catobj = $db->setQuery($query)->loadObject();

		if($contentobj->metadesc != '') {
			$doc->setMetaData('og:description', $contentobj->metadesc);
		}else{
			if($catobj->metadesc != '') {
				$doc->setMetaData( 'og:description', $catobj->metadesc);
			}else{
				$doc->setMetaData( 'og:description', $config->MetaDesc);
			}

		}

		$content_images = json_decode($contentobj->images);
		$cat_params = json_decode($catobj->params);

		if($content_images->image_intro != '') {
			$doc->setMetaData('og:image', JUri::base() . $content_images->image_intro);
			$doc->setMetaData('og:image:secure_url', JUri::base() . $content_images->image_intro);
		}else{
			if($cat_params->image != '') {
				$doc->setMetaData('og:image', JUri::base() . $cat_params->image);
				$doc->setMetaData('og:image:secure_url', JUri::base() . $cat_params->image);
			}else{
				$doc->setMetaData( 'og:image', JUri::base().$params->get('metaimage'));
				$doc->setMetaData( 'og:image:secure_url', JUri::base().$params->get('metaimage'));
			}
		}

		return true;
	}

	function setMetaCategory($option,$view,$layout,$catid,$id,$Itemid,$params){
		$app = JFactory::getApplication();
		$db = JFactory::getDbo();
		$config = JFactory::getConfig();

		$query = 'SELECT * FROM #__categories WHERE id = '.$id;
		$catobj = $db->setQuery($query)->loadObject();

		$doc = JFactory::getDocument();
		$doc->setMetaData( 'og:title', $catobj->title );
		$doc->setMetaData( 'og:site_name', $config->sitename );
		$doc->setMetaData( 'og:url', JURI::current());
		$doc->setMetaData( 'og:type', 'article');
		$doc->setMetaData( 'og:locale', 'vi_VN');

		$doc->setMetaData( 'og:description', $catobj->metadesc);


		$cat_params = json_decode($catobj->params);

		$doc->setMetaData( 'og:image', JUri::base().$cat_params->image);
		$doc->setMetaData( 'og:image:secure_url', JUri::base().$cat_params->image);
	

		return true;
	}
}
